package fr.paris.lutece.plugins.techos.dao.chat;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;

import fr.paris.lutece.plugins.genericjpa.bean.GenericJPAFilter;
import fr.paris.lutece.plugins.genericjpa.dao.GenericJPADAO;
import fr.paris.lutece.plugins.techos.bean.chat.Chat;
import fr.paris.lutece.plugins.techos.bean.chat.ChatFilter;
import fr.paris.lutece.plugins.techos.bean.chat.Chat_;


/**
 * The Chat DAO class
 * @author jchaline
 */
public class ChatDAO extends GenericJPADAO<Integer, Chat> implements IChatDAO
{

    @Override
    protected void buildCriteriaQuery( GenericJPAFilter<Integer> filter, Root<Chat> root, CriteriaQuery<Chat> query,
            CriteriaBuilder builder )
    {
        if ( filter instanceof ChatFilter )
        {
            ChatFilter filterCast = (ChatFilter) filter;
            //Join<Bean, OtherBean> otherBean= root.join( Bean_.otherBean, JoinType.INNER );

            List<Predicate> listPredicates = new ArrayList<Predicate>( );

            if ( StringUtils.isNotBlank( filterCast.getName( ) ) )
            {
                listPredicates.add( builder.like( root.get( Chat_._name ), filterCast.getName( ) ) );
            }

            if ( !listPredicates.isEmpty( ) )
            {
                // add existing predicates to Where clause
                query.where( listPredicates.toArray( new Predicate[0] ) );
            }
        }
    }
}